﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.admin
{
    public partial class teacher : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            addTeacher();
        }

        private void addTeacher()
        {
            String firstName = firstNameTextBox.Text;
            String lastName = lastNameTextBox.Text;
            String email = emailTextBox.Text;
            String password = passwordTextBox.Text;
            String stringPhoneNumber = phoneNumberTextBox.Text;

            int phoneNumber = -1;
            if (stringPhoneNumber.Length > 0)
            {
                phoneNumber = Convert.ToInt32(stringPhoneNumber);
            }

            //ADD teacher user to lpusertable
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var preUser = (from u in db.lpusers
                           where u.user_email == email
                           select u).SingleOrDefault();
            if (preUser == null)
            {
                lpuser user = new lpuser();
                user.user_email = email;
                user.user_password = password;
                user.user_level = 1;
                db.lpusers.AddObject(user);
                db.SaveChanges();


                //GET the new ID from the lpuser
                int userId = (from u in db.lpusers
                              where u.user_email == email
                              select u.user_id).SingleOrDefault();

                //ADD additional info to user_info
                if (phoneNumber != -1)
                {
                    user_info user2 = new user_info()
                    {
                        user_id = userId,
                        user_info_first_name = firstName,
                        user_info_last_name = lastName,
                        user_info_phone_number = phoneNumber
                    };
                    db.user_info.AddObject(user2);
                }
                else
                {
                    user_info user2 = new user_info()
                    {
                        user_id = userId,
                        user_info_first_name = firstName,
                        user_info_last_name = lastName
                    };
                    db.user_info.AddObject(user2);
                }
                
                db.SaveChanges();
                Server.Transfer("teacher.aspx");
            }
            else
            {
                printError("T-postur longu skrásettur");
            }
        }

        protected String getButtons(int user_id, String name)
        {
            String temp = "[";
            temp += "<a href='edit_user.aspx?id=" + user_id + "&p=t'><img src='../images/edit-icon.png' alt='Broyt' width='20' height='20'/></a> | ";
            temp += "<a href='delete_user.aspx?id=" + user_id + "' onclick='return confirm(\"Skal brúkarin " + name + " strikas?\")'>Strika</a>";
            temp += "]";
            return temp;
        }

        private void printError(String s)
        {
            Response.Write(s);
        }
    }
}